package spring.security.browser.config;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import spring.security.browser.support.SimpleResponse;
import spring.security.core.properties.SecurityProperties;

@RestController
public class BrowserSecurityController {

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	//缓存当前请求
	private RequestCache requestCache = new HttpSessionRequestCache();
	//请求跳转的工具类
	private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
	@Autowired
	private ProviderSignInUtils providerSignInUtils;
	
	@Autowired
	private SecurityProperties securityProperties;
	
	/**
	 * 当需要身份认证时,跳转到这里
	 * @param request
	 * @param response
	 * @return	
	 */
	@GetMapping("/auto/require")
	@ResponseStatus(code = HttpStatus.UNAUTHORIZED)
	public SimpleResponse requireAuthentication(HttpServletRequest request, HttpServletResponse response) {
		SavedRequest savedRequest = requestCache.getRequest(request, response);
		if(savedRequest != null) {
			String redirectUrl = savedRequest.getRedirectUrl();
			logger.info("引发跳转到URL : " + redirectUrl);
			
			//if(StringUtils.endsWithIgnoreCase(redirectUrl, ".html")) {
				try {
					redirectStrategy.sendRedirect(request, response, securityProperties.getBrowser().getLoginPage());
				} catch (IOException e) {
					e.printStackTrace();
				}
			//}
		}
		
		return new SimpleResponse("访问受限,请登录!");
	}

	@GetMapping("/social/user")
	public SocialUserInfo getSocialUserInfo(HttpServletRequest request) {
		Connection<?> connection = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request));
		SocialUserInfo info = new SocialUserInfo();
		info.setProviderId(connection.getKey().getProviderId());
		info.setPrividerUserId(connection.getKey().getProviderUserId());
		info.setNickname(connection.getDisplayName());
		info.setHeadimg(connection.getImageUrl());
		return info;
	}
	
	@GetMapping("/session/invalid")
	@ResponseStatus(code = HttpStatus.UNAUTHORIZED)
	public SimpleResponse sessioIinvalid(HttpServletRequest request) {
		String message = "Session失效!!!";
		return new SimpleResponse(message);
	}
	
	
	
}
